package java_test_work04;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class PetShop {
	private static final int INITIAL_CAPACITY = 10;

	private Map<String, Pets[]> petsMap;
	private Scanner input;
	private int petFoot;
	private int num;
	private int inNum;
	private int outNum;
	private String Name;

	public PetShop() {
		this.petsMap = new HashMap<>();
		this.petsMap.put("cat", new Pets[INITIAL_CAPACITY]);
		this.petsMap.put("dog", new Pets[INITIAL_CAPACITY]);
		this.input = new Scanner(System.in);
		this.petFoot = 0;
	}

	public void printView() {
		Date date = new Date();
		System.out.println("===============宠物商店库存清单===============");
		System.out.println("*******************C A T S*******************");
		System.out.println("种类\t颜色\t年龄\t性别\t体型\t进货价格\t销售价格");
		for (Pets pet : petsMap.get("cat")) {
			if (pet != null) {
				System.out.println(String.format("%s\t%s\t%s月\t%s\t%s\t%.2f\t%.2f", pet.getKind(), pet.getColor(),
						pet.getAge(), pet.getSex(), pet.getShape(), pet.getInprice(), pet.getOutprice()));
			}
		}
		System.out.println("\n" + "*******************D O G S*******************");
		System.out.println("种类\t颜色\t年龄\t性别\t体型\t进货价格\t销售价格");
		for (Pets pet : petsMap.get("dog")) {
			if (pet != null) {
				System.out.println(String.format("%s\t%s\t%s月\t%s\t%s\t%.2f\t%.2f", pet.getKind(), pet.getColor(),
						pet.getAge(), pet.getSex(), pet.getShape(), pet.getInprice(), pet.getOutprice()));
			}
		}
		System.out.println("=================================================");
		System.out.println("时间: " + date);
		System.out.println("=================================================");
	}

	public void addPets() {
		System.out.println("您添加的是狗还是猫？\n" + "1.狗 2.猫");
		String choice = input.next();

		if (choice.equals("1")) {
			addDog();
		} else if (choice.equals("2")) {
			addCat();
		} else {
			System.out.println("没有这个选项，请重新输入!");
		}
	}

	private void addCat() {
		Cats cat = new Cats();
		System.out.println("请输入您要添加的宠物的信息");
		System.out.print("种类：");
		cat.setKind(input.next());
		System.out.print("颜色：");
		cat.setColor(input.next());

		int age;
		while (true) {
			System.out.print("年龄：");
			try {
				age = Integer.parseInt(input.next());
				if (age > 0) {
					break;
				} else {
					System.out.println("年龄必须为正整数，请重新输入！");
				}
			} catch (NumberFormatException e) {
				System.out.println("年龄必须为正整数，请重新输入！");
			}
		}
		cat.setAge(age);

		System.out.print("性别：");
		cat.setSex(input.next());
		System.out.print("体型：");
		cat.setShape(input.next());
		System.out.print("进货价格：");
		cat.setInprice(input.nextDouble());

		System.out.print("出售价格：");
		cat.setOutprice(input.nextDouble());

		if (petFoot == petsMap.get("cat").length) {
			int newLength = petsMap.get("cat").length * 2;
			petsMap.put("cat", Arrays.copyOf(petsMap.get("cat"), newLength));
		}

		petsMap.get("cat")[petFoot] = cat;
		petFoot++;
		System.out.println("添加成功！");
	}

	private void addDog() {
		Dogs dog = new Dogs();
		System.out.println("请输入您要添加的宠物的信息");
		System.out.print("种类：");
		dog.setKind(input.next());
		System.out.print("颜色：");
		dog.setColor(input.next());

		int age;
		while (true) {
			System.out.print("年龄：");
			try {
				age = Integer.parseInt(input.next());
				if (age > 0) {
					break;
				} else {
					System.out.println("年龄必须为正整数，请重新输入！");
				}
			} catch (NumberFormatException e) {
				System.out.println("年龄必须为正整数，请重新输入！");
			}
		}
		dog.setAge(age);

		System.out.print("性别：");
		dog.setSex(input.next());
		System.out.print("体型：");
		dog.setShape(input.next());
		System.out.print("进货价格：");
		dog.setInprice(input.nextDouble());

		System.out.print("出售价格：");
		dog.setOutprice(input.nextDouble());

		if (petFoot == petsMap.get("dog").length) {
			int newLength = petsMap.get("dog").length * 2;
			petsMap.put("dog", Arrays.copyOf(petsMap.get("dog"), newLength));
		}

		petsMap.get("dog")[petFoot] = dog;
		petFoot++;
		System.out.println("添加成功！");
	}

	private void addCat(String kind, String color, int age, String sex, String shape, double inprice, double outprice) {
		Cats newCat = new Cats(kind, color, age, sex, shape, inprice, outprice);
		if (petFoot == petsMap.get("cat").length) {
			int newLength = petsMap.get("cat").length * 2;
			petsMap.put("cat", Arrays.copyOf(petsMap.get("cat"), newLength));
		}
		petsMap.get("cat")[petFoot] = newCat;
		petFoot++;
		System.out.println("添加成功！");
	}

	private void addDog(String kind, String color, int age, String sex, String shape, double inprice, double outprice) {
		Dogs newDog = new Dogs(kind, color, age, sex, shape, inprice, outprice);
		if (petFoot == petsMap.get("dog").length) {
			int newLength = petsMap.get("dog").length * 2;
			petsMap.put("dog", Arrays.copyOf(petsMap.get("dog"), newLength));
		}
		petsMap.get("dog")[petFoot] = newDog;
		petFoot++;
		System.out.println("添加成功!");
	}

	// 销售宠物
	public void sell() {
		// 检查库存是否为零
		if (num == 0) {
			System.out.println("库存为零，请及时购进宠物！\n");
		} else {
			// 提示用户选择要出售的宠物类型
			System.out.println("您要出售的是猫还是狗？\n" + "1.猫 2.狗");
			String choice = input.next();

			// 对用户输入进行校验
			if (!choice.equals("1") && !choice.equals("2")) {
				System.out.println("没有这个选项，请重新输入！");
				sell();
				return;
			}

			if (choice.equals("1")) {
				// 出售猫
				sellCat();
			} else if (choice.equals("2")) {
				// 出售狗
				sellDog();
			}
		}
	}

	// 销售猫的方法
	private void sellCat() {
		System.out.println("请输入您要出售的猫的特征");
		System.out.print("种类：");
		String kind = input.next();
		System.out.print("颜色：");
		String color = input.next();
		System.out.print("年龄：");
		int age = input.nextInt();
		System.out.print("性别：");
		String sex = input.next();
		int i, flag = -1;
		// 搜索具有提供特征的猫
		for (i = 0; i < petsMap.get("cat").length; i++) {
			if (petsMap.get("cat")[i] != null && kind.equals(petsMap.get("cat")[i].getKind())
					&& color.equals(petsMap.get("cat")[i].getColor()) && age == petsMap.get("cat")[i].getAge()
					&& sex.equals(petsMap.get("cat")[i].getSex())) {
				flag = i;
				break;
			}
		}
		if (flag == -1) {
			// 给出友好的提示信息，并让用户重新输入特征
			System.out.println("查无此猫！请核对后重新输入 \n");
			sellCat();
			return;
		} else {
			petsMap.get("cat")[flag] = null;
			System.out.println("售出成功！\n");
			outNum++;
			num--;
		}
	}

	// 销售狗的方法
	private void sellDog() {
		System.out.println("请输入您要出售的狗的特征");
		System.out.print("种类：");
		String kind = input.next();
		System.out.print("颜色：");
		String color = input.next();
		System.out.print("年龄：");
		int age = input.nextInt();
		System.out.print("性别：");
		String sex = input.next();
		int i, flag = -1;
		// 搜索具有提供特征的狗
		for (i = 0; i < petsMap.get("dog").length; i++) {
			if (petsMap.get("dog")[i] != null && kind.equals(petsMap.get("dog")[i].getKind())
					&& color.equals(petsMap.get("dog")[i].getColor()) && age == petsMap.get("dog")[i].getAge()
					&& sex.equals(petsMap.get("dog")[i].getSex())) {
				flag = i;
				break;
			}
		}
		if (flag == -1) {
			// 给出友好的提示信息，并让用户重新输入特征
			System.out.println("查无此狗！请核对后重新输入 \n");
			sellDog();
			return;
		} else {
			petsMap.get("dog")[flag] = null;
			System.out.println("售出成功！\n");
			outNum++;
			num--;
		}
	}

	// 修改宠物信息的方法
	public void modifyPets() {
		if (num == 0) {
			System.out.println("当前库存为空，无法修改！");
		} else {
			System.out.println("请选择要修改的宠物类别：\n1.狗 2.猫");
			String choice = input.next();
			if (choice.equals("1")) {
				modifyDog();
			} else if (choice.equals("2")) {
				modifyCat();
			} else {
				System.out.println("没有这个选项，请重新输入!");
			}
		}
	}

	// 修改狗的方法
	public void modifyDog() {
		System.out.println("请输入要修改的狗的特征");
		System.out.print("种类：");
		String kind = input.next();
		System.out.print("颜色：");
		String color = input.next();
		System.out.print("年龄：");
		int age = input.nextInt();
		System.out.print("性别：");
		String sex = input.next();

		int i, flag = -1;
		for (i = 0; i < petsMap.get("dog").length; i++) {
			if (petsMap.get("dog")[i] != null && kind.equals(petsMap.get("dog")[i].getKind())
					&& color.equals(petsMap.get("dog")[i].getColor()) && age == petsMap.get("dog")[i].getAge()
					&& sex.equals(petsMap.get("dog")[i].getSex())) {
				flag = i;
				break;
			}
		}

		if (flag == -1) {
			System.out.println("查无此狗！请核对后重新输入 \n");
		} else {
			System.out.println("请输入新的信息：");
			System.out.print("颜色：");
			String newColor = input.next();
			System.out.print("年龄：");
			int newAge = input.nextInt();

			// 对新的宠物信息进行校验，确保输入数据的正确性
			if (newAge < 0 || newAge > 30) {
				System.out.println("年龄输入不合法，请重新输入！");
				return;
			}

			// 如果输入的数据格式正确，则进行修改操作
			petsMap.get("dog")[flag].setColor(newColor);
			petsMap.get("dog")[flag].setAge(newAge);
			System.out.println("修改成功！\n");
		}
	}

	// 修改猫的方法
	public void modifyCat() {
		System.out.println("请输入要修改的猫的特征");
		System.out.print("种类：");
		String kind = input.next();
		System.out.print("颜色：");
		String color = input.next();
		System.out.print("年龄：");
		int age = input.nextInt();
		System.out.print("性别：");
		String sex = input.next();

		int i, flag = -1;
		for (i = 0; i < petsMap.get("cat").length; i++) {
			if (petsMap.get("cat")[i] != null && kind.equals(petsMap.get("cat")[i].getKind())
					&& color.equals(petsMap.get("cat")[i].getColor()) && age == petsMap.get("cat")[i].getAge()
					&& sex.equals(petsMap.get("cat")[i].getSex())) {
				flag = i;
				break;
			}
		}

		if (flag == -1) {
			System.out.println("查无此猫！请核对后重新输入 \n");
		} else {
			System.out.println("请输入新的信息：");
			System.out.print("颜色：");
			String newColor = input.next();
			System.out.print("年龄：");
			int newAge = input.nextInt();

			// 对新的宠物信息进行校验，确保输入数据的正确性
			if (newAge < 0 || newAge > 30) {
				System.out.println("年龄输入不合法，请重新输入！");
				return;
			}

			// 如果输入的数据格式正确，则进行修改操作
			petsMap.get("cat")[flag].setColor(newColor);
			petsMap.get("cat")[flag].setAge(newAge);
			System.out.println("修改成功！\n");
		}
	}

//	查看盈利统计
	// 盈利情况统计
	public void profitNote() {
		Date date = new Date();
		System.out.println("===========宠物商店盈利情况===========");
		double cost = calculateCost();
		double income = calculateIncome();
		double myProfit = income - cost;

		System.out.println("成本：" + cost + "\n" + "总收入：" + income + "\n" + "利润：" + myProfit);
		if (myProfit > 0) {
			System.out.println("恭喜，您的店处于盈利状态！");
		} else {
			System.out.println("很遗憾，您的店处于亏损状况！");
		}
		System.out.println("=================================================");
		System.out.println("时间: " + date);
		System.out.println("=================================================");
	}

//销售记录
	// 销售统计
	public void salesNote() {
		Date date = new Date();
		System.out.println("===============宠物商店销售记录清单===============");
		System.out.println("种类\t颜色\t年龄\t性别\t体型\t进货价格\t销售价格");

		printPets(petsMap.get("cat"));
		printPets(petsMap.get("dog"));

		System.out.println("=================================================");
		System.out.println("时间: " + date);
		System.out.println("=================================================");
	}

	private void printPets(Pets[] pets) {
		if (pets != null) {
			for (Pets pet : pets) {
				if (pet != null) {
					System.out.println(String.format("%s\t%s\t%s月\t%s\t%s\t%.2f\t%.2f", pet.getKind(), pet.getColor(),
							pet.getAge(), pet.getSex(), pet.getShape(), pet.getInprice(), pet.getOutprice()));
				}
			}
		}
	}

	private double calculateCost() {
		double cost = 0.0;
		for (Pets pet : petsMap.get("cat")) {
			if (pet != null) {
				cost += pet.getInprice();
			}
		}

		for (Pets pet : petsMap.get("dog")) {
			if (pet != null) {
				cost += pet.getInprice();
			}
		}
		return cost;
	}

	private double calculateIncome() {
		double income = 0.0;
		for (Pets pet : petsMap.get("cat")) {
			if (pet != null) {
				income += pet.getOutprice();
			}
		}

		for (Pets pet : petsMap.get("dog")) {
			if (pet != null) {
				income += pet.getOutprice();
			}
		}
		return income;
	}

	public int getOutNum() {
		return outNum;
	}

	public int getInNum() {
		return inNum;
	}

	public int getNum() {
		return num;
	}

	public void setName(String Name) {
		this.Name = Name;
	}
}
